#include "category.h"

#include <QXmlStreamReader>

Widget::Widget(const QString &name,const QString &c,const QIcon &icon):
    m_name(name),
    m_icon(icon),
    m_class(c)
{
}

void Widget::setIcon(const QIcon &icon)
{
    m_icon=icon;
}

void Widget::setName(const QString &name)
{
    m_name=name;
}

void Widget::setClassName(const QString &c)
{
    m_class=c;
}

QString Widget::name() const
{
    return m_name;
}

QString Widget::className()const
{
    return m_class;
}

QIcon Widget::icon() const
{
    return m_icon;
}

void Widget::setUI(const QString &ui)
{
    m_ui=ui;
    QXmlStreamReader r(ui);
    while(!r.atEnd())
    {
        if(r.readNext()==QXmlStreamReader::StartElement)
        {
            foreach(const QXmlStreamAttribute &attribute,r.attributes())
            {
                QStringRef name=attribute.name();
                if(name==QLatin1String("class"))
                {
                    setClassName(attribute.value().toString());
                    return;
                }
            }
        }
    }
}

QString Widget::ui()
{
    return m_ui;
}

bool Widget::isNull()
{
    return m_name.isEmpty();
}

Category::Category(const QString &name):
    m_name(name)
{
}

QString Category::name() const
{
    return m_name;
}

void Category::setName(const QString &name)
{
    m_name=name;
}

Widget Category::widget(int idx) const
{
    if(idx<0 || idx>=m_widget_list.size())
    {
        return Widget();
    }
    return m_widget_list.at(idx);
}

int Category::widgetCount() const
{
    return m_widget_list.size();
}

void Category::addWidget(const Widget &widget)
{
    m_widget_list.append(widget);
}
